package com.neusoft.ecommerce.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.ecommerce.dbhelper.LayuiPager;
import com.neusoft.ecommerce.entity.UserAddress;
import com.neusoft.ecommerce.entity.UserMessage;
import com.neusoft.ecommerce.service.UserAddressService;
import com.neusoft.ecommerce.service.UserMessageService;

/**
 * 
 * @author MaoMao
 *
 */
@RestController
public class UserAddressController {
	private final UserAddressService userAddressService;
	private final UserMessageService userMessageService;
	
	
	public UserAddressController(UserAddressService userAddressService,UserMessageService userMessageService) {
		this.userAddressService = userAddressService;
		
		this.userMessageService = userMessageService;
	}
	@GetMapping("/userAddress_page")
    public ModelAndView afterSaleServicePage() {
        
         return new ModelAndView("userAddress/userAddress-list");
    }
	
    @RequestMapping("/userAddress/list")
    @ResponseBody
    public LayuiPager<UserAddress> list(@RequestParam(value = "page", defaultValue = "1") String page,
                                    @RequestParam(value = "limit", defaultValue = "10") String limit) {
        System.out.println(page + "" + limit);
        LayuiPager<UserAddress> pager = new LayuiPager<>(Integer.parseInt(page), Integer.parseInt(limit));
        int count = userAddressService.findTotal(pager);
        List<UserAddress> list = userAddressService.findList(pager);
        for (UserAddress userAddress : list) {
            System.out.println(userAddress);
        }
        pager.setData(list);
        pager.setCount(count);
        pager.setCode(0);
        pager.setMsg("");
        return pager;
    }
	
	@GetMapping("/addUserAddress")
    public ModelAndView UserAddress() {
        return new ModelAndView("userAddress/userAddress-add");
    }
	
	@RequestMapping("/userAddress/userMessageList")
    public List<UserMessage> findUserMessageList() {
	    return userMessageService.findAll();
    }
	
	
	@PostMapping("/userAddress/add")
    public Map<String, Object> add(UserAddress userAddress) {
        System.out.println(userAddress);
        Map<String, Object> map = new HashMap<>(16);
        if (userAddressService.add(userAddress)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	
	@GetMapping("/userAddress/edit_page")
    public ModelAndView editPage(UserAddress userAddress, Model model) {
        System.out.println(userAddress);
        LayuiPager<UserAddress> pager = new LayuiPager<>(1,1);
        pager.setCondition(userAddress);
        UserAddress message = userAddressService.findList(pager).get(0);
        System.out.println(message);
        ModelAndView view = new ModelAndView("userAddress/userAddress-edit");
        model.addAttribute("userAddress", message);
        return view;
    }


	@PostMapping("/userAddress/edit")
    public Map<String, Object> edit(UserAddress userAddress) {
        System.out.println(userAddress);
        Map<String, Object> map = new HashMap<>(16);
        if (userAddressService.edit(userAddress)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	@PostMapping("/userAddress/delete")
    public Map<String, Object> delete(UserAddress userAddress) {
        System.out.println(userAddress);
        Map<String, Object> map = new HashMap<>(16);
        if (userAddressService.delete(userAddress)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }

}
